package com.efounder.adapter;

import android.content.Context;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.bean.Address;
import com.efounder.mobilemall.R;
import com.efounder.utils.ResStringUtil;
import com.efounder.view.CommonSwipeMenuLayout;

import java.util.List;

/**
 * Created by will on 18-3-15.
 */

public class SelectAddressAdapter extends RecyclerView.Adapter<SelectAddressAdapter.AddressViewHolder>{

    private List<Address> addresses;
    private Context mContext;

    private OnAddressSelectedListener onAddressSelectedListener;
    private OnShowEditAddressListener onShowEditListener;

    public SelectAddressAdapter(List<Address> addresses, Context mContext) {
        this.addresses = addresses;
        this.mContext = mContext;
    }

    @Override
    public AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_address_list, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final AddressViewHolder holder, final int position) {
        holder.tvName.setText(addresses.get(position).getUserName());
        holder.tvPhone.setText(addresses.get(position).getPhone());

        holder.swipeMenuLayout.setSwipeEnable(false);

        if (addresses.get(position).getIsSelected().equals("1")){
            holder.tvAddress.setText(ResStringUtil.getString(R.string.mail_frame_default) + addresses.get(position).getArea() + " "
                    + addresses.get(position).getAddress());
        }else {
            holder.tvAddress.setText(addresses.get(position).getArea() + " "
                    + addresses.get(position).getAddress());
        }
        holder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onShowEditListener.onShowEditPop(position);
            }
        });
        if (addresses.get(position).isCurrentSelected()){
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.contacts_header_background_color));
            holder.ivSelected.setVisibility(View.VISIBLE);
        }else {
            holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.contact_item_background_color));
            holder.ivSelected.setVisibility(View.INVISIBLE);
        }
        holder.llAddressContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onAddressSelectedListener.onAddressSelected(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return addresses.size();
    }


    class AddressViewHolder extends RecyclerView.ViewHolder{

        ImageView ivEdit;
        ImageView ivSelected;
        TextView tvName;
        TextView tvPhone;
        TextView tvAddress;
        LinearLayout llAddressContent;
        CommonSwipeMenuLayout swipeMenuLayout;

        public AddressViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPhone = (TextView) itemView.findViewById(R.id.tv_phone);
            tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            ivEdit = (ImageView) itemView.findViewById(R.id.iv_edit_address);
            ivSelected = (ImageView) itemView.findViewById(R.id.iv_selected);
            llAddressContent = itemView.findViewById(R.id.ll_address_content);
            swipeMenuLayout = itemView.findViewById(R.id.sml_swipe);
        }
    }

    public interface OnAddressSelectedListener{
        void onAddressSelected(int position);
    }

    public void setOnAddressSelectedListener(OnAddressSelectedListener onAddressSelectedListener) {
        this.onAddressSelectedListener = onAddressSelectedListener;
    }

    public interface OnShowEditAddressListener{
        void onShowEditPop(int position);
    }

    public void setOnShowEditListener(OnShowEditAddressListener onShowEditPopupListener) {
        this.onShowEditListener = onShowEditPopupListener;
    }
}